package leetcode160;

import domain.ListNode;

import java.util.HashMap;


public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int length1 = 0;
        int length2 = 0;
        ListNode tempA = headA;
        ListNode tempB = headB;
        while (tempA != null) {
            length1++;
            tempA = tempA.next;
        }
        while (tempB != null) {
            length2++;
            tempB = tempB.next;
        }
        if (tempA != tempB) {
            return null;
        }
        int count = length1 - length2;
        if (count > 0) {
            for (int i = 0; i < count; i++) {
                headA = headA.next;
            }
        } else if (count < 0) {
            count = -count;
            for (int i = 0; i < count; i++) {
                headB = headB.next;
            }
        }
        while (headA != null) {
            if (headA == headB) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
}
//public class Solution {
//    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//        HashMap<ListNode,Boolean> map = new HashMap<>();
//        while(headA!=null){
//            map.put(headA,Boolean.TRUE);
//            headA = headA.next;
//        }
//        while(headB!=null){
//            Boolean aBoolean = map.get(headB);
//            if(aBoolean!=null && aBoolean){
//                return headB;
//            }
//            headB = headB.next;
//        }
//        return null;
//    }
//}